#include"../server.hpp"

class EchoServer{
    private:
    TcpServer _server;
    private:
    void OnConnected(const PtrConnection& conn){
        DEB_LOG("New conntection:%p",conn.get());
    }
    void OnClosed(const PtrConnection& conn){
        DEB_LOG("Close conntection:%p",conn.get());
    }
    void OnMessage(const PtrConnection& conn,Buffer* buffer){
        conn->Send(buffer->ReadPosition(),buffer->ReadAbleSize());
        buffer->MoveReadOffset(buffer->ReadAbleSize());
        conn->Shutdown();   //这个开启的话，调用一次这个函数后这个tcp连接就会断开
    }
    public:
    EchoServer(int port):_server(port){
        _server.SetThreadCount(2);
        _server.EnableInactiveRelease(10);
        _server.SetConnectedCallback(std::bind(&EchoServer::OnConnected,this,std::placeholders::_1));
        _server.SetMessageCallback(std::bind(&EchoServer::OnMessage,this,std::placeholders::_1,std::placeholders::_2));
        _server.SetClosedCallback(std::bind(&EchoServer::OnClosed,this,std::placeholders::_1));
    }
    void Start(){
        _server.Start();
    }
};